import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  dialog: false,
  user: {},
  device: 'desktop',
  miniChangeObj: {
    activityFlag: false,
    courseFlag: false,
    hotItemsFlag: false,
    modulesFlag: false,
    comboFlag: false,
    bannerFlag: false,
    sportFlag: false,
    venueBookFlag: false,
    homeModuleFlag: false,
    teacherFlag: false,
    seckillFlag: false,
    discountFlag: false,

  },
  miniCompareMy: {
    ModuleFlag: false,
    fns: false
  },
  tabberFlag: false,
  themeFlag: false,
  messageCount: 0,
  messageList: []
}

const mutations = {
  CHANGE_MINIFlAG(state, { code, flag }) {
    state.miniChangeObj[code] = flag
  },
  CHANGE_messageCount(state, payload) {
    state.messageCount = payload.totalCount
    state.messageList = payload.recordList
  },
  COMPARE_MY(state, { code, flag }) {
    state.miniCompareMy[code] = flag
  },
  COMPARE_TABBAR(state, flag) {
    state.tabberFlag = flag
  },
  COMPARE_THEME(state, flag) {
    state.themeFlag = flag
  },
  RESET_MINI(state) {
    for (const i in state.miniChangeObj) {
      state.miniChangeObj[i] = false
    }
    for (const i in state.miniCompareMy) {
      state.miniCompareMy[i] = false
    }
    state.tabberFlag = false
    state.themeFlag = false
  },
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  TOUSER: (state, user) => {
    state.user = user
  }
}

const actions = {
  changeMessageCount({ commit }, payload) {
    commit('CHANGE_messageCount', payload)
  },
  changeMiniFlag({ commit }, config) {
    commit('CHANGE_MINIFlAG', config)
  },
  compareMyAction({ commit }, config) {
    commit('COMPARE_MY', config)
  },
  compareTabbarAction({ commit }, flag) {
    commit('COMPARE_TABBAR', flag)
  },
  compareThemeAction({ commit }, flag) {
    commit('COMPARE_THEME', flag)
  },
  resetMiniFlag({ commit }) {
    commit('RESET_MINI')
  },
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  touUser({ commit }, device) {
    commit('TOUSER', device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
